package com.example.inputnumberview;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class InputNumberView extends RelativeLayout {

    private OnNumberChangeListener onNumberChangeListener;
    private int mCurrentNumber;
    private int mMin, mMax;//这里就不弄设置和获取这两个属性了
    private Button minusBtn, plusBtn;
    private EditText valueEdit;

    public InputNumberView(Context context) {//ctrl+左击多选
        this(context, null);
    }

    public InputNumberView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InputNumberView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs);
        initView(context);
        setUpEvent();
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        //ctrl+t会出现内容的提示
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.InputNumberView);
        mMin = typedArray.getInt(R.styleable.InputNumberView_min, 0);//第二个参数是默认值
        mMax = typedArray.getInt(R.styleable.InputNumberView_max, 100);
        Toast.makeText(context, "" + mMin, Toast.LENGTH_SHORT).show();
        mCurrentNumber = mMin;//初始值默认为最小值
        typedArray.recycle();
    }

    private void setUpEvent() {
        minusBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCurrentNumber <= mMin) return;
                mCurrentNumber--;
                updateText();
            }
        });

        plusBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCurrentNumber >= mMax) return;
                mCurrentNumber++;
                updateText();
            }
        });
    }

    private void initView(Context context) {
        //前两个用this，不管用哪个构造方法都会来到第三个
        //把布局载入进来
        //View view = LayoutInflater.from(context).inflate(R.layout.input_number_view, this, true);
        //View view = LayoutInflater.from(context).inflate(R.layout.input_number_view, this);
        View view = LayoutInflater.from(context).inflate(R.layout.input_number_view, this, false);//this是这个view，3是绑定到这个view(默认为true）
        addView(view);//这3种等价

        minusBtn = this.findViewById(R.id.minus_btn);
        plusBtn = this.findViewById(R.id.plus_btn);
        valueEdit = this.findViewById(R.id.value_edit);

        updateText();
    }

    //右击，Generate->Gettet and Setter
    public int getmNumber() {
        return mCurrentNumber;
    }

    //右击，Generate->Gettet and Setter
    public void setmNumber(int value) {
        this.mCurrentNumber = value;
        this.updateText();
    }

    private void updateText() {
        valueEdit.setText(String.valueOf(mCurrentNumber));
        if (onNumberChangeListener != null) {
            onNumberChangeListener.onNumberChange(this.mCurrentNumber);
        }
    }

    public void setNumberChangeListener(OnNumberChangeListener listener) {
        this.onNumberChangeListener = listener;
    }

    public interface OnNumberChangeListener {
        void onNumberChange(int value);
    }
}
